package com.e3.basdoc.cust.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.cust.model.CustAccountModel;
import com.e3.basdoc.cust.model.CustModel;
import com.e3.template.model.TGFFormModel;
import com.e3.template.tgf.TGFTabFormPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public class CustFormPanel extends TGFTabFormPanel<CustModel> {

	private final static String[] BASE_INFO = { CustModel.ID, CustModel.CODE, CustModel.NAME, CustModel.SHORT_NAME, CustModel.TYPE,
			CustModel.PARENT_CUST, CustModel.REGION, CustModel.BUSINESS_ADDRESS, CustModel.CONTACT_ADDRESS, CustModel.TELPHONE,
			CustModel.FAX, CustModel.ZIPCODE, CustModel.EMAIL, CustModel.REMARK };

	private final static String[] REGISTER_INFO = { CustModel.REGISTER_ADDRESS, CustModel.LEGAL_PERSON, CustModel.LEGAL_PERSON_IDCARD,
			CustModel.REGISTER_NO, CustModel.TAX_NO, CustModel.CONTACT_PERSON, CustModel.CONTACT_WAY, CustModel.FOUND_TIME,
			CustModel.REGISTER_START_TIME, CustModel.REGISTER_END_TIME, CustModel.CURRENCY, CustModel.TAXPAYER_TYPE };
	
//	private final static String[] BANK_ACCOUNT_INFO = {CustModel.BANK_ACCOUNTS};
	
	private CustAccountGridPanel accountGridPanel;

	@Override
	public List<TGFFormModel> getFormModels() {
		List<TGFFormModel> formModels = new ArrayList<TGFFormModel>();
		
		String name = CustMessages.INSTANCE.tab_base_info();
		List<Field<?>> fields = CustHelper.INSTANCE.getFormFields(BASE_INFO);
		formModels.add(new TGFFormModel(0, name, fields));
		
		name = CustMessages.INSTANCE.tab_register_info();
		fields = CustHelper.INSTANCE.getFormFields(REGISTER_INFO);
		formModels.add(new TGFFormModel(1, name, fields));
//		
//		name = CustMessages.INSTANCE.tab_bank_info();
//		fields = CustHelper.getFormFields(BANK_ACCOUNT_INFO);
//		formModels.add(new TGFFormModel(2, name, fields));
		
		return formModels;

	}
	
	private CustAccountGridPanel getAccountGridPanel(){
		if(accountGridPanel==null){
			accountGridPanel = new CustAccountGridPanel();
		}
		return accountGridPanel;
	}
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		
		TabPanel tp = getContentPanel();
		TabItem item = new TabItem(CustMessages.INSTANCE.tab_bank_info());
		item.setLayout(new FitLayout());
		item.add(getAccountGridPanel());
		tp.add(item);
	}

	@Override
	public CustModel createModelInstance() {
		return CustHelper.INSTANCE.createModelInstance();
	}

	@Override
	public void clearFormData() {
		super.clearFormData();
		getAccountGridPanel().clearData();
	}

	@Override
	public void setFormValue(CustModel model) {
		super.setFormValue(model);
		List<CustAccountModel> accountModels = model.getCustAccountList();
		getAccountGridPanel().setGridValues(accountModels);
	}

	@Override
	public CustModel getFormValue() {
		CustModel model = super.getFormValue();
		List<CustAccountModel> accountModels = getAccountGridPanel().getGridValues();
		model.setCustAccountList(accountModels);
		return model;
	}

}
